package www.study.com;

//字符串相乘 https://leetcode.cn/problems/multiply-strings/
public class code43 {
    public static void main(String[] args) {

    }

    class Solution {
        //方式二 详见二哥
        public String multiply(String num1, String num2) {
            StringBuilder sbu = new StringBuilder(num1);
            num1 = sbu.reverse().toString();
            sbu = new StringBuilder(num2);
            num2 = sbu.reverse().toString();
            int[] res = new int[num1.length() + num2.length() + 1];
            for(int i = 0 ; i < num1.length() ; i++){
                for(int j = 0 ; j < num2.length() ; j++){
                    res[i + j] += (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
                }
            }
            for(int i = 0 ; i < num1.length() + num2.length() ; i++){
                if(res[i] >= 10){ //注意这里是大于等于10
                    res[i + 1] += res[i] / 10;
                    res[i] %= 10;
                }
            }
            int reg = num1.length() + num2.length();
            while(res[reg] == 0 && reg > 0){
                reg--;
            }
            sbu = new StringBuilder();
            for( ; reg > -1 ; reg--){
                sbu.append(res[reg]);
            }
            return sbu.toString();
        }
    /*
    //方式一
    public String multiply(String num1, String num2) {
        if(num1.equals("0") || num2.equals("0")) return "0";
        StringBuilder sbu = new StringBuilder();
        String jinweiString = "";
        for(int i = num2.length() - 1 ; i > -1 ; i--){
            int sigNum = num2.charAt(i) - '0';
            //System.out.println("sigNum: " + sigNum);
            //num1 * sigNum
            String cur = "";
            int jinwei = 0;
            for(int j = num1.length() - 1 ; j > -1 ; j--){
                int nowNum = sigNum * (num1.charAt(j) - '0') + jinwei;
                //System.out.println("nowNum: " + nowNum);
                cur += (nowNum % 10); // + '0';
                //System.out.println("cur: " + cur);
                jinwei = nowNum / 10;
            }
            while(jinwei != 0){
                cur += (jinwei % 10); // + '0';
                jinwei /= 10;
            }
            //System.out.println("cur: " + cur);
            //sbu.append(cur.charAt(0));
            //cur = cur.substring(1);
            //cur + jinweiString
            jinwei = 0;
            int k = 0;
            String sumStr = "";
            // if(k < cur.length() || k < jinweiString.length()){
            //     System.out.println("k is " + k);
            // }else{
            //     System.out.println("k not in");
            // }
            while(k < cur.length() || k < jinweiString.length()){

                int nowNum = (k >= cur.length()) ? 0 : (cur.charAt(k) - '0');
                nowNum += (k >= jinweiString.length()) ? 0 : (jinweiString.charAt(k) - '0');
                nowNum += jinwei;
                sumStr += (nowNum % 10);
                jinwei = nowNum / 10;
                k++;
            }
            //System.out.println("sumStr " + sumStr);
            while(jinwei != 0){
                sumStr += (jinwei % 10);
                jinwei /= 10;
            }
            sbu.append(sumStr.charAt(0));
            jinweiString = sumStr.substring(1);
            //System.out.println("jinweiString: " + jinweiString);
        }
        for(int i = 0 ; i <jinweiString.length() ; i++){
            sbu.append(jinweiString.charAt(i));
        }
        return sbu.reverse().toString();
    }*/
    }
}
